package com.wskh.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wskh.edu.entity.Subject;
import com.wskh.edu.entity.excel.SubjectData;
import com.wskh.edu.entity.subject.OneSubject;
import com.wskh.edu.entity.subject.TwoSubject;
import com.wskh.edu.listener.SubjectExcelListener;
import com.wskh.edu.mapper.SubjectMapper;
import com.wskh.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author wskh
 * @since 2021-10-14
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public void saveSubject(MultipartFile file, SubjectService subjectService) {
        InputStream inputStream = null;
        try {
            // 文件输入流
            inputStream = file.getInputStream();
            // 调用方法进行读取
            ExcelReaderBuilder read = EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(subjectService));
            read.sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public List<OneSubject> getAllSubject() {

        List<OneSubject> resultSubjectList = new ArrayList<>();

        //获取一级分类数据记录
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        queryWrapper.orderByAsc("sort", "id");
        List<Subject> oneSubjectList = baseMapper.selectList(queryWrapper);

        //获取二级分类数据记录
        QueryWrapper<Subject> queryWrapper2 = new QueryWrapper<>();
        // ne:不等于
        queryWrapper2.ne("parent_id", 0);
        queryWrapper2.orderByAsc("sort", "id");
        List<Subject> twoSubjectList = baseMapper.selectList(queryWrapper2);

        //填充一级分类vo数据
        int count = oneSubjectList.size();
        for (int i = 0; i < count; i++) {
            Subject subject1 = oneSubjectList.get(i);

            //创建一级类别vo对象
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(subject1, oneSubject);
            resultSubjectList.add(oneSubject);

            //填充二级分类vo数据
            ArrayList<TwoSubject> childrenList = new ArrayList<>();
            int count2 = twoSubjectList.size();

            for (int j = 0; j < count2; j++) {
                Subject subject2 = twoSubjectList.get(j);
                if (subject1.getId().equals(subject2.getParentId())) {
                    //创建二级类别对象
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subject2, twoSubject);
                    childrenList.add(twoSubject);
                }
            }

            oneSubject.setChildren(new ArrayList<>(childrenList));

        }
        return resultSubjectList;
    }
}
